//
//  WebViewController.m
//  xgt
//
//  Created by  程 强 on 15/7/1.
//  Copyright (c) 2015年 Demon. All rights reserved.
//

#import "WebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "JTCSM.h"
#import <WebKit/WebKit.h>

@interface WebViewController ()<UIWebViewDelegate,WKNavigationDelegate,WKUIDelegate>
{
    BOOL isF;
    UIView *theView;
    WKWebView *_mywebView;
}
@property (nonatomic,strong) WebViewJavascriptBridge*bridge;
@end

@implementation WebViewController

//返回父控制器
- (void)gotoBack{
//    if([self.isruzhu isEqualToString:@"wangzheguanjia"]){
//       [self.navigationController popToRootViewControllerAnimated:YES];
//    }else{
//       [self.navigationController popViewControllerAnimated:YES];
//    }
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [df isRemoveTarbar:YES];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    UIView *stateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWidth, Height_StatusBar)];
    stateView.backgroundColor = [df colorWithHexString:@"0fa6e8" alpha:1];
    [self.view addSubview:stateView];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 开启日志，方便调试
//    [WebViewJavascriptBridge enableLogging];
    
    CGFloat space = 0;
    _mywebView = [[WKWebView alloc]initWithFrame:CGRectMake(space, Height_StatusBar, KCONTROLLERWith, KCONTROLLERHeight-Height_StatusBar)];
    _mywebView.scrollView.bounces = NO;
    [_mywebView  setUserInteractionEnabled: YES ];  //是否支持交互
    _mywebView.navigationDelegate = self;
    _mywebView.UIDelegate = self;
    NSURLRequest *request;
    if(self.urlStr == nil){
        // 应用场景:加载从服务器上下载的文件,例如pdf,或者word,图片等等文件
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"index.txt" withExtension:nil];
        request = [NSURLRequest requestWithURL:fileURL];
    }else{
        NSURL *url = [NSURL URLWithString:self.urlStr];
        request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    }
    NSLog(@"url:%@",self.urlStr);
    _mywebView.backgroundColor = [UIColor whiteColor];
    [self showWaiting:_mywebView];
    [_mywebView loadRequest:request];
    [self.view addSubview:_mywebView];
//    // 给哪个webview建立JS与OjbC的沟通桥梁
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:_mywebView];
    // 设置代理，如果不需要实现，可以不设置
    [self.bridge setWebViewDelegate:self];
    
    [self.bridge registerHandler:@"getBlogNameFromObjC" handler:^(id data, WVJBResponseCallback responseCallback) {
        if ([[data allKeys] containsObject:@"user"] && KUSERDEFAULTGET(KuserInfo)==nil) {
            NSDictionary *dict = [data objectForKey:@"user"];
            if ([[dict allKeys] containsObject:@"data"]) {
                KUSERDEFAULTSET([dict objectForKey:@"data"], KuserInfo);
                KUSERDEFAULTSET(dict, Khtml_user);
            }
        }
        if ([[data objectForKey:@"blogURL"] isEqualToString:@"MyList.html"] || [[data objectForKey:@"blogURL"] isEqualToString:@"Mylist.html"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        if ([[data objectForKey:@"blogURL"] isEqualToString:@"loginIndex.html"]) {
            KUSERDEFAULTRemoveAll(KuserInfo);
            KUSERDEFAULTRemoveAll(Khtml_user);
            [self.navigationController popViewControllerAnimated:NO];
            [df gotoLoginVC];
        }
        if (responseCallback) {
            // 反馈给JS
            NSLog(@"---html_user:%@",KUSERDEFAULTGET(Khtml_user));
            responseCallback(@{@"user":(KUSERDEFAULTGET(Khtml_user)==nil?@"":KUSERDEFAULTGET(Khtml_user)),@"phone":self.phone==nil?@"":self.phone});
        }
    }];
    // Do any additional setup after loading the view.
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题，动态改变
    [self hideWaiting:_mywebView];

}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     [self hideWaiting:_mywebView];
}

//

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)layerWithView:(UIView *)view radius:(CGFloat)cornerRadiu width:(CGFloat)borderWidth color:(UIColor *)borderColor{
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = cornerRadiu;
    view.layer.borderWidth = borderWidth;
    view.layer.borderColor = borderColor.CGColor;
}
-(UIActivityIndicatorView *)showWaiting:(UIView *)parent {
    
    int width = 32, height = 32;
    
    CGRect frame = [parent frame]; //[[UIScreen mainScreen] applicationFrame];
    int x = frame.size.width;
    int y = frame.size.height;
    CGFloat changesize = 10;
    frame = CGRectMake((x - width-changesize) / 2, (y - height-changesize) / 2, width+changesize, height+changesize);
    UIView *theV = [[UIView alloc] initWithFrame:frame];
    theV.backgroundColor = [UIColor clearColor];
    [self layerWithView:theV radius:4 width:0 color:nil];
    frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
    UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];
    progressInd.center = CGPointMake(theV.frame.size.width/2, theV.frame.size.height/2);
    [progressInd startAnimating];
    progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    //    frame = CGRectMake((x - 70)/2, (y - height) / 2 + height, 80, 20);
    //    UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
    //    waitingLable.text = @"Loading...";
    //    waitingLable.textColor = [UIColor whiteColor];
    //    waitingLable.font = [UIFont systemFontOfSize:15];
    //    waitingLable.backgroundColor = [UIColor clearColor];
    
    frame =  CGRectMake(0, 0, x, y);
    theView = [[UIView alloc] initWithFrame:frame];
    theView.backgroundColor = [UIColor clearColor];
    
    
    [theV addSubview:progressInd];
    //    [theView addSubview:waitingLable];
    
    
    [theView addSubview:theV];
    [parent addSubview:theView];
    [parent bringSubviewToFront:theView];
    
    //    [[XGTSingle shareSingle].rootView addSubview:theView];
    //    [progressInd insertSubview:theView atIndex:1];
    
    return progressInd;
}
//消除滚动轮指示器
-(void)hideWaiting:(UIView *)parent
{
    [theView removeFromSuperview];
    theView = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
